package com.sys.menu;

import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import net.sf.navigator.displayer.MenuDisplayerMapping;
import net.sf.navigator.displayer.MessageResourcesMenuDisplayer;
import net.sf.navigator.menu.MenuComponent;

/**
 * @author 
 * @version $Revision: 1.2 $
 */
public class TreeMenuDisplayer extends MessageResourcesMenuDisplayer {

	private final static MessageFormat MENU_BEGIN = new MessageFormat(
			"<ul id=\"{0}\" style=\"display:none;\">");

	// private final static MessageFormat MENU_TOP = new MessageFormat("<li>");

	// private final static MessageFormat MENU_BOTTOM = new
	// MessageFormat("</li>");

	private final static MessageFormat MENU_MAIN_ITEM = new MessageFormat(
			"<li><span>{0}<div id=\"Spacer\" style=\"position:relative; height:4px; width:1px; font-size:1px;\">&nbsp;</div></span><ul style=\"border-top: 2px dotted #DEF0A1; display:none;\">{3}</ul></li>");

	private final static MessageFormat MENU_EXPANDER_ITEM = new MessageFormat(
			"<li><span>{0}</span><ul>{3}</ul></li>");

	private final static MessageFormat MENU_STANDALONE = new MessageFormat(
			"<li><span>{0}<div id=\"Spacer\" style=\"position:relative; height:4px; width:1px; font-size:1px;\">&nbsp;</div></span><ul style=\"border-top: 2px dotted #DEF0A1; display:none;\"><li><a>{3}</a></ul></li>");

	private final static MessageFormat MENU_ITEM = new MessageFormat(
			"<li><a href=\"{1}\" title=\"\"><img src=\"{2}\" class=\"bullet\"/>{0}</a></li>");

	// private final static MessageFormat MENU_END = new MessageFormat("</ul>");

	// private final static MessageFormat IMG = new MessageFormat(
	// "<img src=\"{0}\" class=\"bullet\"/>");

	private String contextPath = "";

	public void init(PageContext context, MenuDisplayerMapping mapping) {
		super.init(context, mapping);
		HttpServletRequest request = (HttpServletRequest) context.getRequest();
		contextPath = request.getContextPath();
		if (contextPath == null || contextPath.equals("/")) {
			contextPath = "";
		}
		String id = (String) context.getAttribute("menuId");
		try {
			out.println(MENU_BEGIN.format(new Object[] { id }));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	public void end(PageContext context) {
		try {
			out.print("</ul>");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	/**
	 * @see net.sf.navigator.displayer.MessageResourcesMenuDisplayer#display(net.sf.navigator.menu.MenuComponent)
	 */
	public void display(MenuComponent menu) throws JspException, IOException {
		StringBuffer sb = new StringBuffer();
		addMenuString(menu, sb, isAllowed(menu));
		out.println("\n    ");
		out.println(sb);
	}

	protected void addMenuString(MenuComponent menu, StringBuffer sb,
			boolean allowed) {
		if (allowed) {
			StringBuffer subsb = new StringBuffer();

			MenuComponent[] subMenus = menu.getMenuComponents();

			if (subMenus.length > 0) {

				for (int i = 0; i < subMenus.length; i++) {
					addMenuString(subMenus[i], subsb,
							(allowed) ? isAllowed(subMenus[i]) : allowed);
				}

				if (subsb.length() > 0) {
					if (menu.getParent() == null) {
						sb.append(MENU_MAIN_ITEM.format(new Object[] {
								menu.getTitle(), menu.getLocation(),
								menu.getImage(), subsb.toString() }));
					} else {
						sb.append(MENU_EXPANDER_ITEM.format(new Object[] {
								menu.getTitle(), menu.getLocation(),
								menu.getImage(), subsb.toString() }));
					}
				} else {
					if (menu.getParent() == null) {
						sb.append(MENU_STANDALONE.format(new Object[] {
								menu.getTitle(), menu.getLocation(),
								menu.getImage(), getMessage("tmd.menu.hasNotPermissions") }));
					}
				}
			} else {
				if (menu.getParent() == null) {
					sb.append(MENU_STANDALONE.format(new Object[] {
							menu.getTitle(), menu.getLocation(),
							menu.getImage(), getMessage("tmd.menu.isDeveloping") }));
				} else {
					sb.append(MENU_ITEM.format(new Object[] { menu.getTitle(),
							menu.getLocation(),
							getUrl("/resources/images/bullet_creme.gif") }));
				}
			}
		}
	}

	private String getUrl(String originalUrl) {
		return originalUrl != null ? contextPath + originalUrl : "";
	}

}
